﻿using System;
using System.Reflection;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MugenInjection.Infrastructure;
using MugenInjection.Parameters;
using TestModels.Class;
using System.Linq;
using Should;

namespace MugenInjectionTest.Parameters
{
    [TestClass]
    public class FieldParameterTest
    {
        [TestMethod]
        public void CanResolveNamedTest()
        {
#if NETFX_CORE
            var fieldInt = typeof(ParameterTestModel).GetTypeInfo().DeclaredFields.First(c => c.Name == "PublicFieldInt");
            var fieldString = typeof(ParameterTestModel).GetTypeInfo().DeclaredFields.First(c => c.Name == "PublicFieldString");
#else
            var fieldInt = typeof(ParameterTestModel).GetField("PublicFieldInt");
            var fieldString = typeof(ParameterTestModel).GetField("PublicFieldString");
#endif

            var parameter = new FieldParameter("PublicFieldInt", int.MaxValue);
            parameter.CanResolve(fieldString, null).ShouldBeFalse();
            parameter.CanResolve(fieldInt, null).ShouldBeTrue();

            parameter = new FieldParameter("PublicFieldInt", context => int.MaxValue);
            parameter.CanResolve(fieldString, null).ShouldBeFalse();
            parameter.CanResolve(fieldInt, null).ShouldBeTrue();

            parameter = new FieldParameter("PublicFieldInt", typeof(int), 0);
            parameter.CanResolve(fieldString, null).ShouldBeFalse();
            parameter.CanResolve(fieldInt, null).ShouldBeTrue();

            parameter = new FieldParameter("PublicFieldInt", context => string.Empty, typeof(int));
            parameter.CanResolve(fieldString, null).ShouldBeFalse();
            parameter.CanResolve(fieldInt, null).ShouldBeTrue();

            parameter = new FieldParameter("PublicFieldInt", typeof(bool), 0);
            parameter.CanResolve(fieldString, null).ShouldBeFalse();
            parameter.CanResolve(fieldInt, null).ShouldBeFalse();

            parameter = new FieldParameter("PublicFieldInt", context => string.Empty, typeof(bool));
            parameter.CanResolve(fieldString, null).ShouldBeFalse();
            parameter.CanResolve(fieldInt, null).ShouldBeFalse();

            parameter = new FieldParameter("PublicFieldInt1", typeof(int), 0);
            parameter.CanResolve(fieldString, null).ShouldBeFalse();
            parameter.CanResolve(fieldInt, null).ShouldBeFalse();

            parameter = new FieldParameter("PublicFieldInt1", context => string.Empty, typeof(int));
            parameter.CanResolve(fieldString, null).ShouldBeFalse();
            parameter.CanResolve(fieldInt, null).ShouldBeFalse();
        }

        [TestMethod]
        public void GetValueTest()
        {
            var value = new object();
            var value1 = new object();

            var parameter = new FieldParameter("param1", value);
            parameter.GetValue(new ParameterContext(null, null)).ShouldEqual(value);

            parameter = new FieldParameter("param1", context => value1);
            parameter.GetValue(new ParameterContext(null, null)).ShouldEqual(value1);
        }
    }
}